package ye.guo.huang.java11;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;

/**
 * @Author: HUANG GY.
 * @Date: 2021/11/10 15:45
 * @Description: ye.guo.huang.java10
 */
public class Test01Http {

    public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
//        HttpClient client = HttpClient.newHttpClient();
//        HttpRequest request = HttpRequest.newBuilder(URI.create("http://www.baidu.com")).build();
//        HttpResponse.BodyHandler<String> responseBodyHandler = HttpResponse.BodyHandlers.ofString();
//        HttpResponse<String> response = client.send(request, responseBodyHandler);
//        String body = response.body();
//        System.out.println(body);

        sync();

    }

    private static void sync() throws ExecutionException, InterruptedException {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder(URI.create("http://www.baidu.com")).build();
        HttpResponse.BodyHandler<String> responseBodyHandler = HttpResponse.BodyHandlers.ofString();
        CompletableFuture<HttpResponse<String>> sendAsync = client.sendAsync(request, responseBodyHandler);
        HttpResponse<String> response = sendAsync.get();
        String body = response.body();
        System.out.println(body);
        System.out.println(1);
    }

}
